import { createApp } from 'vue'
// import './style.css'
import App from './App.vue'
import router from "./router";
import store from "./store";
import "../src/logistics.css";
//导入axios
import axios from "axios";
import VueAxios from "vue-axios";
//统一的跨域前缀
axios.defaults.baseURL ='http://localhost:3688/';
//设置默认的请求超时时间。例如超过了5s，就会告知用户当前请求超时，请刷新等。
axios.defaults.timeout = 5000;

//引入ElementUI
import ElementUI from 'element-plus'
import 'element-plus/dist/index.css'
//改中文
import locale from "element-plus/lib/locale/lang/zh-cn"
//1、引用图标库
import * as ElIcons from '@element-plus/icons-vue';

import { ElMessage } from 'element-plus'
import {useStore} from "vuex";

const app = createApp(App);
app.use(router)
app.use(store)
app.use(VueAxios,axios)
app.use(ElementUI,{locale})
// 前置拦截
axios.interceptors.request.use(function(config) {
    console.log(localStorage.getItem("token"))
    if(localStorage.getItem("token")=='null' || localStorage.getItem("token")==null){
        console.log(1)
        router.push("/login");
    }else{
        config.headers.Authorization =localStorage.getItem("token")
    }
    return config;
}, function(error) {
    return Promise.reject(error);
});
//后置拦截
axios.interceptors.response.use(response => {
    let res = response.data;
    // console.log("=================")
    console.log(res)
    // console.log("=================")
    localStorage.setItem("token",res.token)
    store.state.token = res.token;
    if(res.code==200){
        return response
    }else if(res.code==402){
        ElMessage({
            message: "账号或密码错误！",
            type: 'warning',
        })
        return response
        router.push("/login");
    }else if(res.code==403){
        // ElMessage({
        //     message: "身份过期，请重新登录！",
        //     type: 'warning',
        // })
        router.push("/login");
        return response
    }else if(res.code==401){
        ElMessage({
            message: "您的权限不足！",
            type: 'warning',
        })
    }
});
for(const [key,comp] of Object.entries(ElIcons)){
    app.component(key,comp)
}
app.mount('#app');


// createApp(App).mount('#app')
